import FirstStore from '../../../store/firstStore'
import {upDate} from './index'
import mapData from './mapData'
import {toJS} from "mobx";

const _ = require('lodash');

const geoCoordMap = mapData

const XAData = [
    [{name: '北京市'}, {name: '大兴区', value: 100}],
    // [{name: '西安'}, {name: '北京', value: 100}],
    // [{name: '西安'}, {name: '上海', value: 100}],
    // [{name: '西安'}, {name: '广州', value: 100}],
    // [{name: '西安'}, {name: '西宁', value: 100}],
    // [{name: '西安'}, {name: '银川', value: 100}]
];

const XNData = [
    // [{name: '西宁'}, {name: '北京', value: 100}],
    [{name: '西宁市'}, {name: '上海市', value: 100}],
    [{name: '西宁市'}, {name: '广州市', value: 100}],
    [{name: '西宁市'}, {name: '西安市', value: 100}],
    [{name: '西宁市'}, {name: '银川市', value: 100}]
];

const YCData = [
    // [{name: '银川'}, {name: '北京', value: 100}],
    [{name: '银川市'}, {name: '广州市', value: 100}],
    [{name: '银川市'}, {name: '上海市', value: 100}],
    [{name: '银川市'}, {name: '西安市', value: 100}],
    [{name: '银川市'}, {name: '西宁市', value: 100}],
];

let planePath = 'path://M1705.06,1318.313v-89.254l-319.9-221.799l0.073-208.063c0.521-84.662-26.629-121.796-63.961-121.491c-37.332-0.305-64.482,36.829-63.961,121.491l0.073,208.063l-319.9,221.799v89.254l330.343-157.288l12.238,241.308l-134.449,92.931l0.531,42.034l175.125-42.917l175.125,42.917l0.531-42.034l-134.449-92.931l12.238-241.308L1705.06,1318.313z';
//var planePath = 'arrow';
let convertData = function (data) {
    let res = [];
    for (let i = 0; i < data.length; i++) {

        let dataItem = data[i];

        let fromCoord = geoCoordMap[dataItem[0].name];
        let toCoord = geoCoordMap[dataItem[1].name];
        if (fromCoord && toCoord) {
            res.push({
                fromName: dataItem[0].name,
                toName: dataItem[1].name,
                coords: [fromCoord, toCoord],
                value: dataItem[1].value
            });
        }
    }
    return res;

};

// 管理series数据
let matchSeries = () => {
    const color = ['#a6c84c', '#ffa022', '#46bee9'];//航线的颜色
    let series = [];
    // ['西安市', XAData], ['西宁市', XNData], ['银川市', YCData]
    [['北京市', FirstStore.flyLineOne]].forEach((item, i) => {
        series.push({
                name: item[0] + ' Top3',
                type: 'lines',
                zlevel: 1,
                effect: {
                    show: true,
                    period: 6,
                    trailLength: 0.7,
                    color: 'red',   //arrow箭头的颜色
                    symbolSize: 5
                },
                lineStyle: {
                    normal: {
                        color: color[i],
                        width: 0,
                        curveness: 0.2
                    }
                },
                data: convertData(item[1])
            },
            {
                name: item[0] + ' Top3',
                type: 'lines',
                zlevel: 2,
                symbol: ['none', 'arrow'],
                symbolSize: 10,
                effect: {
                    show: true,
                    period: 6,
                    trailLength: 0,
                    symbol: planePath,
                    symbolSize: 10
                },
                lineStyle: {
                    normal: {
                        color: color[i],
                        width: 3,
                        opacity: 0.6,
                        curveness: 0.2
                    }
                },
                data: convertData(item[1])
            },
            {
                name: item[0] + ' Top3',
                type: 'effectScatter',
                coordinateSystem: 'geo',
                zlevel: 2,
                rippleEffect: {
                    brushType: 'stroke'
                },
                label: {
                    normal: {
                        show: false,
                        position: 'right',
                        formatter: '{b}'
                    }
                },
                symbolSize: function (val) {
                    return 5;
                },
                itemStyle: {
                    normal: {
                        color: color[i],
                    },
                    emphasis: {
                        areaColor: '#2B91B7'
                    }
                },
                data: item[1].map(function (dataItem) {
                    return {
                        name: dataItem[1].name,
                        value: geoCoordMap[dataItem[1].name].concat([dataItem[1].value])
                    };
                })
            });
    })
    series.push(
        {
            type: 'effectScatter',
            coordinateSystem: 'geo',
            zlevel: 1,
            rippleEffect: {
                brushType: 'stroke'
            },
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    formatter: '{b}'
                }
            },
            symbolSize: function (val) {
                return 10;
            },
            itemStyle: {
                normal: {
                    color: 'rgb(0,128,0)',
                },
                emphasis: {
                    areaColor: '#2B91B7'
                }
            },
            data: FirstStore.lightAll
        },
        {
            type: 'map',
            geoIndex: 0,
        }, {
            name: 'left',
            type: 'effectScatter',
            coordinateSystem: 'geo',
            zlevel: 2,
            rippleEffect: {
                brushType: 'stroke'
            },
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    formatter: '{b}'
                }
            },
            symbolSize: function (val) {
                return 20;
            },
            itemStyle: {
                normal: {
                    color: 'rgb(236,44,38)',
                },
                emphasis: {
                    areaColor: '#2B91B7'
                }
            },
            data: FirstStore.redArr
        }, {
            name: 'left',
            type: 'effectScatter',
            coordinateSystem: 'geo',
            zlevel: 2,
            rippleEffect: {
                brushType: 'stroke'
            },
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    formatter: '{b}'
                }
            },
            symbolSize: function (val) {
                return 15;
            },
            itemStyle: {
                normal: {
                    color: 'rgb(255,255,0)',
                },
                emphasis: {
                    areaColor: '#2B91B7'
                }
            },
            data: FirstStore.yellowArr
        }, {
            name: 'left',
            type: 'effectScatter',
            coordinateSystem: 'geo',
            zlevel: 2,
            rippleEffect: {
                brushType: 'stroke'
            },
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    formatter: '{b}'
                }
            },
            symbolSize: function (val) {
                return 20;
            },
            itemStyle: {
                normal: {
                    color: 'rgb(236,44,38)',
                },
                emphasis: {
                    areaColor: '#2B91B7'
                }
            },
            data: FirstStore.redArrLog
        }, {
            name: 'left',
            type: 'effectScatter',
            coordinateSystem: 'geo',
            zlevel: 2,
            rippleEffect: {
                brushType: 'stroke'
            },
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    formatter: '{b}'
                }
            },
            symbolSize: function (val) {
                return 15;
            },
            itemStyle: {
                normal: {
                    color: 'rgb(255,255,0)',
                },
                emphasis: {
                    areaColor: '#2B91B7'
                }
            },
            data: FirstStore.yellowArrLog
        })
    setTimeout(() => {
        !FirstStore.isUpDate &&
        upDate()
        FirstStore.isUpDate = true
    }, 5000)
    return series
}

// 匹配Modal数据
let matchData = function (northeastArea, eastArea, southArea, westArea, northArea, name) {
    let tooTip
    let arr = [
        ['黑龙江', '吉林', '辽宁'], ['北京', '天津', '内蒙古', '河南', '河北', '山东', '山西', '吉林', '辽宁'],
        ['重庆', '四川', '陕西', '甘肃', '宁夏', '新疆', '青海', '西藏'], ['广东', '广西', '湖北', '湖南', '福建', '云南', '贵州', '海南'],
        ['上海', '江苏', '浙江', '安徽']
    ]
    let arrD = [northeastArea, northArea, westArea, southArea, eastArea]
    arr.forEach((item, index) => {
        if (item.some(item1 => {
            return item1 === name
        })) {
            tooTip = addData(arrD[index]);
        }
    })
    return tooTip
}

// 匹配Regions数据
let matchDataRegions = function (northeastArea, eastArea, southArea, westArea, northArea, name) {
    let regions = []
    let arr = [
        ['黑龙江', '吉林', '辽宁'], ['北京', '天津', '内蒙古', '河南', '河北', '山东', '山西', '吉林', '辽宁'],
        ['重庆', '四川', '陕西', '甘肃', '宁夏', '新疆', '青海', '西藏'], ['广东', '广西', '湖北', '湖南', '福建', '云南', '贵州', '海南'],
        ['上海', '江苏', '浙江', '安徽']
    ]
    arr.forEach((item, index) => {
        if (item.some(item1 => {
            return item1 === name
        })) {
            FirstStore.dataPre = arr[index]
            arr[index].forEach((item2) => {
                regions.push({
                    name: item2,
                    itemStyle: {
                        areaColor: 'rgb(2,216,252)',
                        color: 'red'// 单个区域的样式设置
                    },
                })
            })
            FirstStore.arr = regions
            upDate()
        }
    })
    return FirstStore.arr
}

// 拼接Modal数据
let addData = function (area) {
    if (!area.PreferredServiceTeam) {
        return
    }
    return area.PreferredServiceTeam + '<br />' + 'LocaltionNum:' + area.LocaltionNum + '<br />' + 'PreferredTechnicianNum:' + area.PreferredTechnicianNum + '<br />' + 'SecondaryTechnicianNum:' + area.SecondaryTechnicianNum + '<br />' + area.list.map((item) => {
        return (
            'TDSPlatform:' + item.TDSPlatform + '&nbsp;&nbsp;&nbsp;' + 'num:' + item.num + '<br />'
        )
    }).join('')
}


// 渲染中国地图的配置
export const optionConfig = (northeastArea, eastArea, southArea, westArea, northArea) => {
    return {
        tooltip: {
            trigger: 'item',
            enterable: true,
            position: 'top',
            formatter: function (params, ticket, callback) {
                if (
                    (params.seriesType === "effectScatter" && parseFloat(params.data[0]) >= 115.416 && parseFloat(params.data[0]) <= 117.50 && parseFloat(params.data[1]) >= 39.433 && parseFloat(params.data[1]) <= 41.05) ||
                    (params.seriesType === "effectScatter" && parseFloat(params.data[0]) >= 120.85 && parseFloat(params.data[0]) <= 117.2 && parseFloat(params.data[1]) >= 39.666 && parseFloat(params.data[1]) <= 41.883)
                ) {
                    const {macNumArr, macNumArrLog, numAll} = FirstStore
                    const arr = [...macNumArr, ...macNumArrLog, ...numAll]
                    let arrM = []
                    let arrC = []
                    let arrS = []
                    let arrSS = []
                    arr.forEach((item, index) => {
                        // 北京
                        if (parseFloat(item.key[0]) >= 115.416 && parseFloat(item.key[0]) <= 117.50 && parseFloat(item.key[1]) >= 39.433 && parseFloat(item.key[1]) <= 41.05) {
                            arrM.push(item.value)
                        } else if (parseFloat(item.key[0]) >= 117.2 && parseFloat(item.key[0]) <= 120.85 && parseFloat(item.key[1]) >= 39.666 && parseFloat(item.key[1]) <= 41.883) {
                            arrS.push(item.value) // 上海
                        }
                    })
                    arrM.forEach((item1) => {
                        arrC.push(`<a href="#/second?SerialLotNumber=${item1}">${item1}</a><br />`.replace(',', ''))
                    });
                    arrS.forEach((item2) => {
                        arrSS.push(`<a href="#/second?SerialLotNumber=${item2}">${item2}</a><br />`.replace(',', ''))
                    });
                    if (parseFloat(params.data[0]) >= 115.416 && parseFloat(params.data[0]) <= 117.50 && parseFloat(params.data[1]) >= 39.433 && parseFloat(params.data[1]) <= 41.05) {
                        return _.join(arrC, '')
                    }
                    if (parseFloat(params.key[0]) >= 117.2 && parseFloat(params.key[0]) <= 120.85 && parseFloat(params.key[1]) >= 39.666 && parseFloat(params.key[1]) <= 41.883) {
                        return _.join(arrSS, '')
                    }
                } else if (params.seriesType === "lines") {
                    return params.data.fromName + ">" + params.data.toName + "<br />";
                } else { // seriesType === 'map'
                    // 避免同一区域多次刷新,避免多一个大区域多次刷新
                    if ((params.name !== FirstStore.name) && !FirstStore.dataPre.some(item => {
                        return item === params.name
                    })) {
                        matchDataRegions(northeastArea, eastArea, southArea, westArea, northArea, params.name)
                    }
                    const name = params.name
                    FirstStore.name = name
                    return matchData(northeastArea, eastArea, southArea, westArea, northArea, name)
                }
            }
        },
        geo: {
            map: 'china',
            label: {
                show: true,
                textStyle: {
                    color: '#FFFFFF', //地图初始化区域字体颜色
                    fontSize: 10,
                    opacity: 1,
                    backgroundColor: 'rgba(0,23,11,0)'
                },
                emphasis: {
                    show: false,
                    color: '#fff'
                },
            },
            roam: false,
            zoom: 1.2,
            itemStyle: {
                normal: {
                    areaColor: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [{
                            offset: 0, color: '#004ea2' // 0% 处的颜色
                        }, {
                            offset: 1, color: '#09a0d1' // 100% 处的颜色
                        }],
                        global: false // 缺省为 false
                    },
                    borderColor: 'transparent',
                    borderWidth: 1,
                },
                emphasis: {
                    show: false,
                    areaColor: 'rgb(2,216,252)',
                }
            },
            regions: FirstStore.arr,
        },
        series: matchSeries()
    }
}
